package com.lczyfz.istep.modules.openstack.api.neutron.impl.firewall;

import com.lczyfz.istep.modules.openstack.api.neutron.impl.parseJson.NeutronParse;
import com.lczyfz.istep.modules.openstack.api.neutron.interfaces.firewall.FireWallRules;
import com.lczyfz.istep.modules.openstack.api.neutron.model.firewall.FireWallRule;
import com.lczyfz.istep.modules.openstack.constvary.ConstParams;
import com.lczyfz.istep.modules.openstack.constvary.RequestType;
import com.lczyfz.istep.modules.openstack.utils.HTTP;
import com.lczyfz.istep.modules.openstack.utils.Request;
import com.lczyfz.istep.modules.openstack.utils.RequestFactory;
import com.lczyfz.istep.modules.openstack.utils.Response;
import net.sf.json.JSONObject;

import java.util.List;

public class FireWallRuleImpl implements FireWallRules {

	public RequestFactory factory = new RequestFactory("neutron");
	@Override
	public List<FireWallRule> getFireWallRules() {
		List<FireWallRule> fwrules = null;
		Request request = factory.getRequest();
		request.setUrl("/v2.0/fw/firewall_rules");
		request.setRequestType(RequestType.GET);
		
		Response results = HTTP.getConnectionResult(request);
		JSONObject body = results.getResponseBody();
//		System.out.println(body.toString());
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}	
		
		fwrules = NeutronParse.resolveFireWallRules(body);
		return fwrules;
	}

	@Override
	public FireWallRule getFireWallRule(String fireWallRuleId) {
		FireWallRule fwrule = new FireWallRule();
		Request request = factory.getRequest();
		request.setUrl("/v2.0/fw/firewall_rules/" + fireWallRuleId);
		request.setRequestType(RequestType.GET);
		
		Response results = HTTP.getConnectionResult(request);
		JSONObject body = results.getResponseBody();
	//	System.out.println(body.toString());
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		
		fwrule = NeutronParse.resolveFireWallRuleDetail(body);
		return fwrule;
	}

	@Override
	public FireWallRule createFireWallRule(FireWallRule fireWallRule) {
		FireWallRule fwrule = new FireWallRule();
		JSONObject params = NeutronParse.packageFireWallRule(fireWallRule);
		System.out.println(params.toString());
		Request request = factory.getRequest();
		request.setUrl("/v2.0/fw/firewall_rules");
		request.setRequestType(RequestType.POST);
		request.setBody(params);
		
		Response results = HTTP.getConnectionResult(request);
		if( ConstParams.RESPONSE_CODE_CREATED!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		
		fwrule = NeutronParse.resolveFireWallRuleDetail(res);
		return fwrule;
	}

	@Override
	public void deleteFireWallRule(String fireWallRuleId) {
		Request request = factory.getRequest();
		request.setUrl("/v2.0/fw/firewall_rules/" + fireWallRuleId);
		request.setRequestType(RequestType.DELETE);
		
		Response results = HTTP.getConnectionResult(request);
		if(ConstParams.RESPONSE_CODE_SUCCESS  != results.getResponseCode() )
		{
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		return;
	}

	@Override
	public FireWallRule updateFireWallRule(String fireWallRuleId, FireWallRule fireWallRule) {
		FireWallRule fwrule = new FireWallRule();
		JSONObject params = NeutronParse.packageFireWallRule(fireWallRule);
		System.out.println(params.toString());
	//	System.out.println("/v2.0/fwaas/firewall_rules/" + FwRuleId);
		Request request = factory.getRequest();
		request.setUrl("/v2.0/fw/firewall_rules/" + fireWallRuleId);
		request.setRequestType(RequestType.PUT);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);	
		
		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		
		fwrule = NeutronParse.resolveFireWallRuleDetail(res);
		return fwrule;
	}
	
	public static void main(String[] args) {
		FireWallRuleImpl fwrule = new FireWallRuleImpl();


		
		
//		FireWallRule fwruledetail = fwrule.getFireWallRule("5986b9a6-90d1-4bb0-bfd7-df7c69d98f34");
//		System.out.println(fwruledetail.getId()+" "+fwruledetail.getName()+" "+fwruledetail.getAction()+" "+fwruledetail.getDestinationIpAddress()+
//				" "+fwruledetail.getDestinationPort()+" "+fwruledetail.getFirewallPolicyId()+" "+fwruledetail.getProtocol()+" "+
//				fwruledetail.getSourceIpAddress()+" "+fwruledetail.getSourcePort());
////		
//		
		FireWallRule fwruledemo = new FireWallRule();
		fwruledemo.setAction("deny");
		fwruledemo.setProtocol("tcp");
		fwruledemo.setIpVersion("4");
		fwruledemo.setName("f1");
		fwruledemo.setDestinationIpAddress("10.10.20.10/24");
		fwruledemo.setSourceIpAddress("10.10.10.30/24");

		FireWallRule newfwrule = fwrule.createFireWallRule(fwruledemo);
		List<FireWallRule> fwrulelist = fwrule.getFireWallRules();
		for(FireWallRule fr : fwrulelist) {
			System.out.println(fr.getId()+" "+fr.getName()+" "+fr.getAction()+" "+fr.getDestinationIpAddress()+
					" "+fr.getDestinationPort()+" =1= "+fr.getFirewallPolicyId()+" "+fr.getProtocol()+" "+
					fr.getSourceIpAddress()+" "+fr.getSourcePort());
		}
//		
//		FireWallRule fwruleupdate = new FireWallRule();
//		fwruleupdate.setDestinationPort("10");
//		fwruleupdate.setSourcePort("50");
//		fwruleupdate.setProtocol("tcp");
//		fwruleupdate.setShared("true");
//		fwruleupdate.setName("ff");
//		FireWallRule updatefwrule = fwrule.updateFireWallRule("5986b9a6-90d1-4bb0-bfd7-df7c69d98f34",fwruleupdate);
//		
//		fwrule.deleteFireWallRule("06a2cef3-9fc6-436a-9e0f-e7d0cc61ed88");
		
		
	}

}
